/***************************************************************************
 *   Copyright (C) 2008 by Tomasz Dubik                                    *
 *   tdubik@gmail.com                                                      *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/

#include "newitemdialogimpl.h"
#include "globals.h"

#include <QDebug>
#include <QString>
#include <QMessageBox>

NewItemDialogImpl::NewItemDialogImpl(QWidget * parent):QDialog(parent),m_task(0)
{
    setupUi(this);
    connect( m_percentDone, SIGNAL(textEdited(const QString&)), this, SLOT(percentChanged(const QString&)));

    m_txt->setFocus();
}


void NewItemDialogImpl::setTask( Task*& task )
{
	m_task = task;
	setTxt(task->taskTxt());
	setPriority(task->priority());
	setPercent(task->percent());
	setType(task->type());
	setDate(task->date());
}

void NewItemDialogImpl::accept()
{
	QString txt = m_txt->toPlainText();

	if( txt.isEmpty() )
	{
		DisplayWarning(tr("Task text should be filled"));
		return;
	}
    
    m_task->setTaskTxt(txt);

    int percent = m_percentDone->text().toInt();
    if( percent > 100 )
    {
        percent = 100;
    }
    m_task->setPercent(percent);
	
    m_task->setType((TaskType)m_type->currentIndex());
    
    m_task->setPriority((TaskPriority)m_priority->currentIndex());

	QDialog::accept();
}

int NewItemDialogImpl::exec()
{
    m_txt->setFocus();
    m_txt->selectAll();
    m_percentDone->setCursorPosition(0);
    return QDialog::exec();
}

void NewItemDialogImpl::reject()
{    
	QDialog::reject();
}

void NewItemDialogImpl::percentChanged(const QString&)
{
    if( m_percentDone->text().toInt() > 100 )
        m_percentDone->setText("100");
}

void NewItemDialogImpl::setPriority(int priority)
{
	m_priority->setCurrentIndex(priority);
}

void NewItemDialogImpl::setType(int type)
{
	m_type->setCurrentIndex(type);
}

void NewItemDialogImpl::setTxt(const QString& txt)
{
	m_txt->setText(txt);
}

void NewItemDialogImpl::setDate(const QDate& date)
{
	m_date = date;
}

void NewItemDialogImpl::setPercent(const int percent)
{
    setPercent(QString::number(percent));
}

void NewItemDialogImpl::setPercent(const QString& percent)
{
    m_percentDone->setText(percent);
}

void NewItemDialogImpl::DisplayWarning(const QString& txt)
{
	QMessageBox::warning(this, "QWhatToDo", txt, QMessageBox::Ok);
}


//EOF
